﻿Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D

'<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
'<ComponentModel.DefaultEvent("AddImage_Click")> _
Public Class RoundCornerLabel
    Inherits System.Windows.Forms.Label

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(Windows.Forms.ControlStyles.SupportsTransparentBackColor, True)
        Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)

    End Sub

#Region "Designer Generated Code"
    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private WithEvents components As System.Windows.Forms.Label

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'components
        '
        Me.components.Location = New System.Drawing.Point(0, 0)
        Me.components.Name = "EdPicureBox"
        Me.components.Size = New System.Drawing.Size(100, 23)
        Me.components.TabIndex = 0
        '
        'PictureBox
        '
        Me.Name = "EdPicureBox"
        Me.ResumeLayout(False)
    End Sub
#End Region

#Region "Declarations"
    'Dim iBorder As Boolean = True
    'Dim iBorderDarkColor As Color = SystemColors.ActiveBorder
    'Dim iBorderLightColor As Color = SystemColors.InactiveBorder
#End Region

    Shadows Property BoderStyle As Windows.Forms.BorderStyle
        Get
            Return Windows.Forms.BorderStyle.None
        End Get
        Set(value As Windows.Forms.BorderStyle)
            MyBase.BorderStyle = Windows.Forms.BorderStyle.None
        End Set
    End Property
    Dim _Borders As BorderStyle = Windows.Forms.BorderStyle.FixedSingle
    Public Property Borders As BorderStyle
        Get
            Return _Borders
        End Get
        Set(value As BorderStyle)
            _Borders = value
            Me.Invalidate()
        End Set
    End Property
    Dim _ParentBackColor As Color = SystemColors.InactiveBorder
    Public Property ParentBackColor As Color
        Get
            Return Me._ParentBackColor
        End Get
        Set(value As Color)
            Me._ParentBackColor = value
            Me.Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaintBackground(e)
        'e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), Me.ClientRectangle)
    End Sub

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaint(e)

        Dim gh As New GraphicsHelper
        Dim aRect As New Rectangle(Me.ClientRectangle.X, _
                                   Me.ClientRectangle.Y, _
                                   Me.ClientRectangle.Width - 1, _
                                   Me.ClientRectangle.Height - 1)

        'Erase background with parent backcolor
        e.Graphics.FillRectangle(New SolidBrush(Me._ParentBackColor), Me.ClientRectangle)

        'Draw background
        Dim bRect As New Rectangle(aRect.X + 1, _
                                   aRect.Y + 1, _
                                   aRect.Width - 2, _
                                   aRect.Height - 2)
        Dim borderPath1 As GraphicsPath = gh.GetRoundCornerPathForRectangle(bRect)
        e.Graphics.FillPath(New SolidBrush(Me.BackColor), borderPath1)

        'Draw border
        If Me._Borders = Windows.Forms.BorderStyle.Fixed3D Then
            e.Graphics.DrawPath(New Pen(RGBHSL.ModifyBrightness(Me.BackColor, 0.5)), _
                        gh.GetRoundCornerPathForRectangle(New Rectangle(aRect.X + 1, _
                                                                        aRect.Y + 1, _
                                                                        aRect.Width - 2, _
                                                                        aRect.Height - 2)))
            e.Graphics.DrawPath(New Pen(RGBHSL.ModifyBrightness(Me.BackColor, 0.3)), _
                    gh.GetRoundCornerPathForRectangle(New Rectangle(aRect.X, _
                                                                    aRect.Y, _
                                                                    aRect.Width - 2, _
                                                                    aRect.Height - 2)))
        ElseIf Me._Borders = Windows.Forms.BorderStyle.FixedSingle Then
            e.Graphics.DrawPath(New Pen(RGBHSL.ModifyBrightness(Me.BackColor, 0.3)), _
                    gh.GetRoundCornerPathForRectangle(New Rectangle(aRect.X, _
                                                                    aRect.Y, _
                                                                    aRect.Width, _
                                                                    aRect.Height)))
        End If

        'Draw text
        Dim tRect As New Rectangle(aRect.X + 1, _
                                   aRect.Y, _
                                   aRect.Width - 3, _
                                   aRect.Height)
        Dim sf As StringFormat = gh.GetTextFormatObjectFromContentAlignment(Me.TextAlign)
        e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), tRect, sf)

    End Sub

    Private Sub RoundCornerLabel_TextChanged(sender As Object, e As System.EventArgs) Handles Me.TextChanged

        If Me.AutoSize Then
            Dim msize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
            If Me._Borders = Windows.Forms.BorderStyle.FixedSingle Then
                Me.Size = New Size(msize.Width + 2, msize.Height + 2)
            ElseIf Me._Borders = Windows.Forms.BorderStyle.Fixed3D Then
                Me.Size = New Size(msize.Width + 3, msize.Height + 3)
            Else
                Me.Size = msize
            End If
        End If

    End Sub

End Class
